﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using MediaBrowserSL.BLL.Contexts;
using MediaBrowserSL.BLL.Tools;
using MediaBrowserSL.BOL;
using MediaBrowserSL.IBLL;
using MediaBrowserSL.IDAL;

namespace MediaBrowserSL.BLL
{
    /// <summary>
    /// Media Manager
    /// </summary>
    public class MediaManager : IMediaManager
    {
        private IMediaRepository mediaRepository;

        public MediaManager(IMediaRepository mediaRepository)
        {
            this.mediaRepository = mediaRepository;
        }

        public IEnumerable<Media> GetListMediasByCategory(int idUser, int idCategory)
        {
            IEnumerable<Media> medias = mediaRepository.GetListMediasByCategory(idUser, idCategory);

            foreach (var m in medias)
            {
                if (!String.IsNullOrEmpty(m.UrlContent)) { m.UrlContent = PathMediaContext.GenericPathMedia + m.User.Login.ToString() + "/" + idCategory + "/" + m.UrlContent; }
                if (!String.IsNullOrEmpty(m.UrlImage)) { m.UrlImage = PathMediaContext.GenericPathMedia + m.User.Login.ToString() + "/" + idCategory + "/" + m.UrlImage; }

                IEnumerable<Media> mediaItems = mediaRepository.GetListMediaItems(m.Id, idUser, idCategory);

                foreach (var mi in mediaItems)
                {
                    if (!String.IsNullOrEmpty(mi.UrlContent)) { mi.UrlContent = PathMediaContext.GenericPathMedia + mi.User.Login.ToString() + "/" + idCategory + "/" + mi.UrlContent; }
                    if (!String.IsNullOrEmpty(mi.UrlImage)) { mi.UrlImage = PathMediaContext.GenericPathMedia + mi.User.Login.ToString() + "/" + idCategory + "/" + mi.UrlImage; }

                    m.MediaItems.Add(mi);
                }
            }

            return medias;
        }

        /// <summary>
        /// Create New Media
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public Media CreateMedia(Media m, User u)
        {
            Media media = mediaRepository.CreateMedia(m, u);

            if (media.UrlContent != null) { m.UrlContent = PathMediaContext.GenericPathMedia + u.Login + "/" + m.Category.Id + "/" + m.UrlContent; }
            if (media.UrlImage != null) { m.UrlImage = PathMediaContext.GenericPathMedia + u.Login + "/" + m.Category.Id + "/" + m.UrlImage; }

            return media;
        }

        /// <summary>
        /// Update  Media
        /// </summary>
        /// <param name="m"></param>
        public void UpdateMedia(Media m)
        {
            mediaRepository.UpdateMedia(m);
        }

        /// <summary>
        /// Delete  Media
        /// </summary>
        /// <param name="m"></param>
        public void DeleteMedia(Media m, User u)
        {
            if (!String.IsNullOrEmpty(m.UrlContent))
            {
                string urlContent = PathMediaContext.DirPathMedia + "/" + u.Login + "/" + m.Category.Id + "/" + Path.GetFileName(m.UrlContent);
                File.Delete(urlContent);
            }

            if (!String.IsNullOrEmpty(m.UrlImage))
            {
                string urlImage = PathMediaContext.DirPathMedia + "/" + u.Login + "/" + m.Category.Id + "/" + Path.GetFileName(m.UrlImage);
                File.Delete(urlImage);
            }

            mediaRepository.DeleteMedia(m);
        }

        /// <summary>
        /// Create Media Item
        /// </summary>
        /// <param name="mediaItem"></param>
        /// <param name="mediaParent"></param>
        /// <param name="u"></param>
        /// <returns></returns>
        public Media CreateMediaItem(Media mediaItem, Media mediaParent, User u)
        {
            Media media = mediaRepository.CreateMediaItem(mediaItem, mediaParent, u);

            if (media.UrlContent != null) { mediaItem.UrlContent = PathMediaContext.GenericPathMedia + u.Login + "/" + mediaItem.Category.Id + "/" + mediaItem.UrlContent; }
            if (media.UrlImage != null) { mediaItem.UrlImage = PathMediaContext.GenericPathMedia + u.Login + "/" + mediaItem.Category.Id + "/" + mediaItem.UrlImage; }

            return media;
        }

        /// <summary>
        /// Convert Xps Book to Png Files
        /// </summary>
        /// <param name="urlBook"></param>
        /// <param name="u"></param>
        /// <returns></returns>
        public IEnumerable<String> GenerateImagesFromPdfBook(String urlPdfBook, User u)
        {
            string xpsFileName = PathMediaContext.DirPathMedia + u.Login.ToString() + @"\4\" + Path.GetFileName(urlPdfBook);
            string pathPng = PathMediaContext.DirPathMedia + u.Login.ToString() + @"\tmp\";

            PDFTools pdfTools = new PDFTools();
            pdfTools.ExtractImagesFromPDF(xpsFileName, pathPng);

            string[] ltmp = Directory.GetFiles(pathPng);
            IComparer comp = new FileComparer(FileComparer.CompareBy.CreationTime);
            Array.Sort(ltmp, comp);

            List<String> lImg = new List<String>();
            foreach (var p in ltmp)
            {
                lImg.Add(PathMediaContext.GenericPathMedia + u.Login.ToString() + "/tmp/" + Path.GetFileName(p));
            }

            return lImg;
        }

        /// <summary>
        /// Get Sum Size By User
        /// </summary>
        /// <param name="u"></param>
        /// <returns></returns>
        public Dictionary<int, double> GetSize(User u)
        {
            Dictionary<int, double> lSizes = new Dictionary<int, double>();

            double serverSpace = Convert.ToDouble(ConfigurationManager.AppSettings["ServerSpace"]);
            double sizeMovie = mediaRepository.GetSizeByCategory(u, 1);
            double sizeMusic = mediaRepository.GetSizeByCategory(u, 2);
            double sizePicture = mediaRepository.GetSizeByCategory(u, 3);
            double sizeBook = mediaRepository.GetSizeByCategory(u, 4);

            double freeSpace = serverSpace - (sizeMovie + sizeMusic + sizePicture + sizeBook);

            lSizes.Add(-1, freeSpace);
            lSizes.Add(1, sizeMovie);
            lSizes.Add(2, sizeMusic);
            lSizes.Add(3, sizePicture);
            lSizes.Add(4, sizeBook);

            return lSizes;
        }
    }
}
